#include "fig_trophie_notification.h"
#include "gen_game_state.h"

#include "ui/ui_manager.h"

FIGTrophieNotification::FIGTrophieNotification() :	m_pUITrophieText(NULL),
													m_pUITrophieImg(NULL),
													m_pUITrophieBox(NULL),
													m_bShowingNotification(false),													
													m_State(STATE_COMING_OUT)
{

}


FIGTrophieNotification&	FIGTrophieNotification::Instance()
{
	static FIGTrophieNotification	Inst;
	return Inst;
}

void		FIGTrophieNotification::ShowNotification(const std::string& Text,
													 const std::string& TrophiImg,
													 int TrophieID)
{
	m_NotificationTexts.push_back(Text);
	m_TrophyImgNames.push_back(TrophiImg);
	m_LastTrophieID =  TrophieID;

	return;



	
}

void		FIGTrophieNotification::Tick(float dt)
{
	if(m_bShowingNotification)
	{
		if(m_pUITrophieBox)
		{
			math::Vec2 Pos =  m_pUITrophieBox->GetPosition();

			if(m_State == STATE_COMING_OUT)
				Pos.x += m_pUITrophieBox->GetSize().x * dt;
			else if(m_State == STATE_GOING_BACK)
				Pos.x -= m_pUITrophieBox->GetSize().x * dt;

			m_pUITrophieBox->SetPosition(Pos);

			if( m_State == STATE_COMING_OUT && Pos.x >= 0)
				m_State = STATE_DELAY;
			
            if( m_State == STATE_GOING_BACK && 
				Pos.x <= -m_pUITrophieBox->GetSize().x)
			{
				m_bShowingNotification = false;				
				if(m_pUITrophieBox)
					m_pUITrophieBox->SetIsVisible(false);
			}
			
			if( m_State == STATE_DELAY)
			{
				m_DelayCounter -= dt;
				if( m_DelayCounter <= 0)
					m_State = STATE_GOING_BACK;				
					
			}

		}
	}
	else if(m_NotificationTexts.size() > 0)
	{
		std::string Text = m_NotificationTexts[0];
		std::string TrophiImg = m_TrophyImgNames[0];
		m_NotificationTexts.erase(m_NotificationTexts.begin());
		m_TrophyImgNames.erase(m_TrophyImgNames.begin());


		if( m_pUITrophieText  == NULL &&
			m_pUITrophieImg == NULL &&
			m_pUITrophieBox == NULL)
		{
			m_pUITrophieText = GENGameState::GetGame()->GetWidget("TrophieText");
			m_pUITrophieImg  = GENGameState::GetGame()->GetWidget("Trophie");
			m_pUITrophieBox  = GENGameState::GetGame()->GetWidget("TrophiePopup");
			if(m_pUITrophieBox)
				m_pUITrophieBox->SetIsVisible(false);
		}

		m_bShowingNotification = true;
		if(m_pUITrophieBox)
			m_pUITrophieBox->SetIsVisible(true);
		m_State = STATE_COMING_OUT;
		m_DelayCounter = 1.0f;

		if(m_pUITrophieText)
			m_pUITrophieText->SetTextA(Text);

		if(m_pUITrophieImg)
		{
			m_pUITrophieImg->SetOffImage(TrophiImg);
			//m_pUITrophieImg->SetAllImage(TrophiImg);		
			
		}
	}
}

void		FIGTrophieNotification::ForceFinish()
{
	m_bShowingNotification = false;

	if( m_pUITrophieText  == NULL &&
		m_pUITrophieImg == NULL &&
		m_pUITrophieBox == NULL)
	{
		m_pUITrophieText = GENGameState::GetGame()->GetWidget("TrophieText");
		m_pUITrophieImg  = GENGameState::GetGame()->GetWidget("Trophie");
		m_pUITrophieBox  = GENGameState::GetGame()->GetWidget("TrophiePopup");
		if(m_pUITrophieBox)
			m_pUITrophieBox->SetIsVisible(false);
	}

	if(m_pUITrophieBox)
		m_pUITrophieBox->SetPosition( 
		math::Vec2(-m_pUITrophieBox->GetSize().x - GraphicsUtil::InvW
												,m_pUITrophieBox->GetPosition().y)
										);
}


